﻿using System;
using System.Collections.Generic;
using IBNet.UtilsDataStructuresAndExtensions;

namespace IBNet.DayTrade.Indicators
{
   public class Stochastic : Indicator
   {
      public Stochastic()
         : base("Stochastic")
      {
      }

      public override decimal? CalculateIndicator(FixedLengthCollection<PriceBar> priceBars)
      {
         PriceBar thisPriceBar = priceBars[0];

         if (thisPriceBar.High == thisPriceBar.Low)
         {
            return 50;
         }
         if (thisPriceBar.Close == thisPriceBar.High)
         {
            return 100;
         }
         if (thisPriceBar.Close == thisPriceBar.Low)
         {
            return 0;
         }

         decimal stochastic = 100 * (thisPriceBar.Close - thisPriceBar.Low) /
                  (thisPriceBar.High - thisPriceBar.Low);
         return stochastic;
      }
   }
}
